package com.atguigu.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.edu.entity.excelUtil.ExcelEventListener;
import com.atguigu.edu.entity.excelUtil.SubjectData;
import com.atguigu.edu.entity.menu.OneSubjectMenu;
import com.atguigu.edu.entity.menu.TwoSubjectMenu;
import com.atguigu.edu.mapper.SubjectMapper;
import com.atguigu.edu.service.SubjectService;
import com.atguigu.edu.entity.Subject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 * 该操作需存在事务 避免部分提交问题
 *
 * @author admin
 * @since 2020-07-05
 */
@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {
    /**
     * 添加课程分类
     *
     * @param file
     * @param subjectService
     * @return
     */
    @Override
    public boolean addSubject(MultipartFile file, SubjectService subjectService) {
        try {
            EasyExcel.read(file.getInputStream(), SubjectData.class, new ExcelEventListener(subjectService)).sheet().doRead();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 查询 课程分类菜单
     *
     * @return
     */
    @Override
    public List<OneSubjectMenu> findAllSubject() {
        QueryWrapper<Subject> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("parent_id", "0");
//        查询一级菜单 parentId为0
        List<Subject> oneSubjectList = baseMapper.selectList(wrapper1);
        wrapper1 = null;
//         查询二级菜单 parentId不为0
        QueryWrapper<Subject> wrapper2 = new QueryWrapper<>();
        wrapper2.ne("parent_id", "0");
        List<Subject> twoSubjectList = baseMapper.selectList(wrapper2);
        wrapper2 = null;
//        一级菜单 数据对象
        ArrayList<OneSubjectMenu> oneSubjectMenuList = new ArrayList<>();

        for (Subject subject : oneSubjectList) {
//        一级菜单所拥有的二级菜单 数据对象
            ArrayList<TwoSubjectMenu> twoSubjectMenuList = new ArrayList<>();

            OneSubjectMenu oneSubjectMenu = new OneSubjectMenu();
//            将值 copy到新的对象中
            BeanUtils.copyProperties(subject, oneSubjectMenu);

            for (Subject twoSubject : twoSubjectList) {
                if (subject.getId().equals(twoSubject.getParentId())) {
                    TwoSubjectMenu twoSubjectMenu = new TwoSubjectMenu();
//            将值 copy到新的对象中
                    BeanUtils.copyProperties(twoSubject, twoSubjectMenu);
                    twoSubjectMenuList.add(twoSubjectMenu);
                    twoSubjectMenu = null;
                }

            }
//            将二级菜单数据对象添加到一级菜单children对象中
            oneSubjectMenu.setChildren(twoSubjectMenuList);
            twoSubjectMenuList = null;
            oneSubjectMenuList.add(oneSubjectMenu);
            oneSubjectMenu = null;
        }
        return oneSubjectMenuList;
    }
}
